//
//  BusinessStoriesViewController.m
//  BaseProject
//
//  Created by 王洋 on 2020/7/4.
//  Copyright © 2020 王洋. All rights reserved.
//
#import "BSMapCell.h"
#import "BSOtherStoreCell.h"
#import "BSIntroduceCell.h"
#import "WYXListCell.h"
#import "BSTableHeaderView.h"

#import "BusinessStoreListViewController.h"

#import "BusinessStoriesViewController.h"

@interface BusinessStoriesViewController ()<UITableViewDelegate, UITableViewDataSource>

@property (nonatomic, strong) UITableView *tableview;

@property (nonatomic, strong) NSMutableArray <CommonSellerModule *>*dataArray;

@end

@implementation BusinessStoriesViewController

- (void)viewDidLoad {
    [super viewDidLoad];

    [self initView];
    [self request_storeList];
}

- (void)viewDidAppear:(BOOL)animated {
    [super viewDidAppear:animated];
    }

- (void)initView {
    
    [self.view addSubview:self.tableview];
    
    [self.tableview mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.top.right.mas_equalTo(0);
        make.bottom.mas_equalTo(0);
    }];
    
}

#pragma mark UITableviewDelegate
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {

    return 3;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    
    switch (section) {
        case BusinessStoriesSectionType_info:{
            return 2;
        } break;
        case BusinessStoriesSectionType_story:{
            return 1;
        } break;
        case BusinessStoriesSectionType_wyx:{
            return self.model.sysWorryFreeLabel.count;
        } break;
        default:
            break;
    }
    return 0;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    switch (indexPath.section) {
        case BusinessStoriesSectionType_info:{

            switch (indexPath.row) {
                case BusinessStoriesCellType_map:{
                    return TTScreenWidth;
                } break;
                case BusinessStoriesCellType_otherStore:{
                    return 60;
                } break;
                default:
                    break;
            }

        } break;
        case BusinessStoriesSectionType_story:{
            
            return [Utilities getStringHeightWithText:self.model.seller.sellerStory font:[UIFont systemFontOfSize:12] viewWidth:(TTScreenWidth - 30)] + 30;

        } break;
        case BusinessStoriesSectionType_wyx:{
//            return 80;
            return [WYXListCell calculateHeightWith:self.model.sysWorryFreeLabel[indexPath.row]];

        } break;
        default:
            break;
    }
    

    return CGFLOAT_MIN;
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    BSTableHeaderView *headView  =  [[NSBundle mainBundle] loadNibNamed:@"BSTableHeaderView"owner:nil options:nil].firstObject;

    switch (section) {
        case BusinessStoriesSectionType_info:{

        } break;
        case BusinessStoriesSectionType_story:{
            headView.itemTitleLab.text = @"商家简介";
        } break;
        case BusinessStoriesSectionType_wyx:{
            headView.itemTitleLab.text = @"放心购商家";
        } break;
        default:
            break;
    }
    return headView;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    
    switch (section) {
        case BusinessStoriesSectionType_info:{
            return CGFLOAT_MIN;
        } break;
        case BusinessStoriesSectionType_story:{

            if (self.model.sysWorryFreeLabel.count > 0) {
                return 50;
            } else {
                return CGFLOAT_MIN;
            }

        } break;
        case BusinessStoriesSectionType_wyx:{
            
            if (self.model.seller.examStatus == 2) {
                return 50;
            } else {
                return CGFLOAT_MIN;
            }
        } break;
        default:
            break;
    }
    return CGFLOAT_MIN;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    switch (indexPath.section) {
        case BusinessStoriesSectionType_info:{

            switch (indexPath.row) {
                case BusinessStoriesCellType_map:{

                    BSMapCell *cell = [tableView dequeueReusableCellWithIdentifier:@"BSMapCellID"];
                    cell.selectionStyle =UITableViewCellSelectionStyleNone;
                    [cell fillCellWithModel:self.model.seller];
                    return cell;
                } break;
                case BusinessStoriesCellType_otherStore:{

                    BSOtherStoreCell *cell = [tableView dequeueReusableCellWithIdentifier:@"BSOtherStoreCellID"];
                    cell.selectionStyle =UITableViewCellSelectionStyleNone;
                    cell.mainTitleLab.text = [NSString stringWithFormat:@"查看其它门店(%ld)",self.dataArray.count];
                    return cell;
                } break;
                default:
                    break;
            }

        } break;
        case BusinessStoriesSectionType_story:{

            BSIntroduceCell *cell = [tableView dequeueReusableCellWithIdentifier:@"BSIntroduceCellID"];
            cell.selectionStyle =UITableViewCellSelectionStyleNone;
            cell.contentLab.text = self.model.seller.sellerStory;
            return cell;

        } break;
        case BusinessStoriesSectionType_wyx:{

            WYXListCell *cell = [tableView dequeueReusableCellWithIdentifier:@"WYXListCellID"];
            cell.selectionStyle =UITableViewCellSelectionStyleNone;

            CommonWYXTagModel *model = self.model.sysWorryFreeLabel[indexPath.row];
            [cell.mainImageView sd_setImageWithURL:[NSURL URLWithString:model.labelUrl] placeholderImage:[UIImage imageNamed:@"placeholder"]];
            cell.mainTitleLab.text = model.labelName;
            cell.subTitleLab.text = model.labelDesc;

            return cell;

        } break;
        default:
            break;
    }
    
    BSIntroduceCell *cell = [tableView dequeueReusableCellWithIdentifier:@"BSIntroduceCellID"];
    cell.selectionStyle =UITableViewCellSelectionStyleNone;
    cell.contentLab.text = self.model.seller.sellerStory;
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    
    switch (indexPath.section) {
        case BusinessStoriesSectionType_info:{

            switch (indexPath.row) {
                case BusinessStoriesCellType_map:{

                } break;
                case BusinessStoriesCellType_otherStore:{

                    if (self.dataArray.count > 0) {
                        BusinessStoreListViewController *vc = [[BusinessStoreListViewController alloc] init];
                        vc.dataArray = @[].mutableCopy;
                        vc.dataArray = self.dataArray;
                        [self.navigationController pushViewController:vc animated:YES];
                    } else {
                        [SVProgressHUD showErrorWithStatus:@"该商家非连锁商家"];
                        [SVProgressHUD dismissWithDelay:1];
                    }
                    
                } break;
                default:
                    break;
            }

        } break;
        case BusinessStoriesSectionType_story:{

        } break;
        case BusinessStoriesSectionType_wyx:{

        } break;
        default:
            break;
    }
}

- (UITableView *)tableview {
    if (!_tableview) {
        _tableview = [[UITableView alloc]initWithFrame:CGRectZero style:UITableViewStylePlain];
        _tableview.delegate = self;
        _tableview.dataSource = self;
        _tableview.separatorStyle = UITableViewCellSeparatorStyleNone;
        _tableview.backgroundColor =  UIColorHex(0xf1f1f1);
        
        _tableview.showsHorizontalScrollIndicator = NO;
        _tableview.showsVerticalScrollIndicator = NO;
        
        _tableview.estimatedSectionHeaderHeight = 0;
        _tableview.estimatedSectionFooterHeight = 0;
        _tableview.estimatedRowHeight = 0;
        
        [_tableview registerClass:[BSMapCell class] forCellReuseIdentifier:@"BSMapCellID"];
        
        [_tableview registerNib:[UINib nibWithNibName:@"BSOtherStoreCell" bundle:nil] forCellReuseIdentifier:@"BSOtherStoreCellID"];
        
        [_tableview registerNib:[UINib nibWithNibName:@"BSIntroduceCell" bundle:nil] forCellReuseIdentifier:@"BSIntroduceCellID"];
        
        [_tableview registerNib:[UINib nibWithNibName:@"WYXListCell" bundle:nil] forCellReuseIdentifier:@"WYXListCellID"];

    }
    return _tableview;
}

//

- (void)request_storeList {
        
    if (self.model.seller.businessId.length == 0) {
        return;
    }
    
    NSMutableDictionary *dic = @{}.mutableCopy;
    
    [dic setValue:@(1) forKey:@"pageNum"];
    [dic setValue:@(20) forKey:@"pageSize"];

    NSMutableDictionary *t = @{}.mutableCopy;
    [t setValue:self.model.seller.businessId forKey:@"businessId"];
    [dic setValue:t forKey:@"t"];

    NSString *url = [NSString stringWithFormat:@"%@%@",TT_Global_Api_Domain,API_StoreList];
    
    WEAKSELF
    [SVProgressHUD showWithStatus:@"正在加载"];
    [TTHttpManager postRequestWithUrl:url TimeOut:10 PostData:dic FinishBlock:^(id  _Nonnull data) {
        
        [SVProgressHUD dismiss];
        if ([data isKindOfClass:[NSDictionary class]]) {

            //请求成功
            NSInteger code = [[data valueForKey:@"code"] integerValue];
            if (code == 10000) {
                NSDictionary *resultDic = [data valueForKey:@"data"];
                if (![resultDic isKindOfClass:[NSDictionary class]]) {
                    return;
                }
                NSArray *list = [resultDic valueForKey:@"list"];

                if ([list isKindOfClass:[NSArray class]]) {
                    weakSelf.dataArray = @[].mutableCopy;
                    for (int i = 0; i < list.count; i++) {
                        NSDictionary *listDic = list[i];
                        CommonSellerModule *model = [CommonSellerModule modelWithDictionary:listDic];

                        [weakSelf.dataArray addObject:model];
                    }
                }

                [weakSelf.tableview reloadData];
            }
        }
        
    } FailedBlock:^(NSError * _Nonnull error) {
        
        [SVProgressHUD dismiss];
    }];
            
}

@end
